﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using log4net;
using ThreeByte.ActiveDeck.Config;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard
{
    /// <summary>
    /// Interaction logic for WizardMainFrame.xaml
    /// </summary>
    public partial class WizardMainFrame : UserControl, ICloseable
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(WizardMainFrame));

        public static CoreConfig ActiveConfig;
        public static ActivityTypeCounter ActiveTypeCounter;

        public WizardMainFrame(CoreConfig config) {
            InitializeComponent();
            ActiveConfig = config;
            ActiveTypeCounter = new ActivityTypeCounter(config);
            ActiveTypeCounter.Refresh();

            LocalNavigationFrame.MainNavigationFrame.Navigated += new NavigatedEventHandler(MainNavigationFrame_Navigated);
        }

        void MainNavigationFrame_Navigated(object sender, NavigationEventArgs e) {
            //Update the page type counts
            ActiveTypeCounter.Refresh();
        }

        public event EventHandler<DialogResultEventArgs> Close;

        private void CloseButton_Click(object sender, RoutedEventArgs e) {
            if(Close != null) {
                Close(this, DialogResultEventArgs.OK);
            }
        }

        private bool _moving = false;
        private Point _downPos;

        private void MoveBorder_MouseDown(object sender, MouseButtonEventArgs e) {
            _moving = true;
            _downPos = e.GetPosition(this);
        }

        public event EventHandler<MoveEventArgs> Move;

        private void MoveBorder_MouseMove(object sender, MouseEventArgs e) {
            if(_moving) {
                Point newPoint = e.GetPosition(this);
                Point offset = new Point(newPoint.X - _downPos.X, newPoint.Y - _downPos.Y);
                log.DebugFormat("Pos: {0}/{1}  New: {2}/{3}", _downPos.X, _downPos.Y, newPoint.X, newPoint.Y);
                RaiseMove(offset);
                BorderPopup.HorizontalOffset = BorderPopup.HorizontalOffset + 1;
                BorderPopup.HorizontalOffset = BorderPopup.HorizontalOffset - 1;
                BorderPopup.InvalidateArrange();
                BorderPopup.UpdateLayout();
                
            }
        }

        private void RaiseMove(Point offset) {
            if(Move != null) {
                Move(this, new MoveEventArgs(offset));
            }
        }

        private void MoveBorder_MouseUp(object sender, MouseButtonEventArgs e) {
            _moving = false;
        }

        private void MoveBorder_MouseEnter(object sender, MouseEventArgs e) {
            _moving = false;
        }

        private void MoveBorder_MouseLeave(object sender, MouseEventArgs e) {
            _moving = false;
        }

        private void MainFrame_Unloaded(object sender, RoutedEventArgs e) {
            ActiveConfig = null; //Don't retain a reference after the wi
        }


    }

    public class MoveEventArgs : EventArgs
    {
        public Point Offset {
            get;
            private set;
        }

        public MoveEventArgs(Point offset) {
            Offset = offset;
        }
    }
}
